//
//  SketchpadsView.m
//  ZYOnlineSchool
//
//  Created by 毕武侠 on 2017/2/24.
//  Copyright © 2017年 bwx. All rights reserved.
//

#import "SketchpadsView.h"
#import "PaletteModel.h"
#import "BHBMyDrawer.h"
#import "BHBDrawer.h"
#import "BHBScrollView.h"


@interface SketchpadsView ()

@property (nonatomic, strong) UIButton *backBtn;
@property (nonatomic, strong) UIButton *clearBtn;
@property (nonatomic, strong) UIButton *nextBtn;
@property (nonatomic, strong) UIButton *lastBtn;

@property (nonatomic, strong) BHBMyDrawer   *myDrawer;
@property (nonatomic, strong) BHBDrawer     *drawer;
@property (nonatomic, strong) BHBScrollView *boardView;

@property (nonatomic, strong) NSMutableDictionary *sketchDatas;    // 存储每页画板数据

@end

@implementation SketchpadsView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initConfigureView];
    }
    return self;
}

- (void)initConfigureView {
    CGFloat width = self.frame.size.width;
    
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 40)];
    headView.backgroundColor = MainThemeColor;
    [self addSubview:headView];
    
    self.backBtn = [[UIButton alloc] initWithFrame:CGRectMake(8, 4, 32, 32)];
    self.backBtn.tag = 10;
    [self.backBtn setImage:[UIImage imageNamed:@"palette_back"] forState:UIControlStateNormal];
    [self.backBtn addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:self.backBtn];
    
    self.nextBtn = [[UIButton alloc] initWithFrame:CGRectMake(width - 60, 0, 40, 40)];
    self.nextBtn.tag = 11;
    [self.nextBtn setImage:[UIImage imageNamed:@"palette_next"] forState:UIControlStateNormal];
    [self.nextBtn addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:self.nextBtn];
    
    self.lastBtn = [[UIButton alloc] initWithFrame:CGRectMake(width - 100, 0, 40, 40)];
    self.lastBtn.tag = 12;
    [self.lastBtn setImage:[UIImage imageNamed:@"palette_last"] forState:UIControlStateNormal];
    [self.lastBtn addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:self.lastBtn];
    
    self.clearBtn = [[UIButton alloc] initWithFrame:CGRectMake(width - 140, 0, 40, 40)];
    self.clearBtn.tag = 13;
    [self.clearBtn setImage:[UIImage imageNamed:@"palette_delete"] forState:UIControlStateNormal];
    [self.clearBtn addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:self.clearBtn];
    
    
    //画板view
    self.boardView = [[BHBScrollView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(headView.frame), self.frame.size.width, self.frame.size.height - CGRectGetMaxY(headView.frame))];
    
    self.boardView.layer.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:.25].CGColor;
    [self.boardView setUserInteractionEnabled:YES];
    [self.boardView setScrollEnabled:YES];
    [self.boardView setMultipleTouchEnabled:YES];
    [self.boardView addSubview:self.drawer];
    [self.boardView setContentSize:self.drawer.frame.size];
    [self.boardView setDelaysContentTouches:NO];
    [self.boardView setCanCancelContentTouches:NO];
    [self insertSubview:self.boardView belowSubview:headView];
    
    @weakify(self)
    [RACObserve(self.drawer, isFirst)  subscribeNext:^(NSNumber *value) {
        @strongify(self)
        self.lastBtn.enabled = ![value boolValue];
    }];
    
    [RACObserve(self.drawer, isLast)  subscribeNext:^(NSNumber *value) {
        @strongify(self)
        self.nextBtn.enabled = ![value boolValue];
    }];
}

- (NSMutableDictionary *)sketchDatas {
    if (_sketchDatas == nil) {
        _sketchDatas = [NSMutableDictionary dictionary];
    }
    return _sketchDatas;
}

- (BHBMyDrawer *)myDrawer {
    if (_myDrawer == nil) {
        _myDrawer = [[BHBMyDrawer alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height*2)];
        _myDrawer.layer.backgroundColor = [UIColor clearColor].CGColor;
    }
    return _myDrawer;
}

- (BHBDrawer *)drawer {
    if (!_drawer) {
        _drawer = [[BHBDrawer alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height*2)];
        _drawer.layer.backgroundColor = [UIColor clearColor].CGColor;
        
    }
    return _drawer;
}

- (PaletteModel *)getModelWithKey:(NSString *)key {
    PaletteModel *model = self.sketchDatas[key];
    if (model == nil) {
        model = [[PaletteModel alloc] init];
        self.sketchDatas[key] = model;
    }
    return model;
}

// 设置页码
- (void)setIndex:(NSUInteger)index {
    _index = index;
    NSString *key = [NSString stringWithFormat:@"%ld", index];
    self.drawer.model = [self getModelWithKey:key];
}

- (void)showWithAnimation:(BOOL)animation {
    if (self.alpha == 1.0) return;
    
    if (animation == NO) {
        self.alpha = 1.0;
    } else {
        [UIView animateWithDuration:0.35 animations:^{
            self.alpha = 1.0;
        }];
    }
}

- (void)hiddenWithAnimation:(BOOL)animation {
    if (self.alpha == 0.0) return;
    
    if (animation == NO) {
        self.alpha = 0.0;
    } else {
        [UIView animateWithDuration:0.35 animations:^{
            self.alpha = 0.0;
        }];
    }
}

/**
 *  按钮点击事件
 *  @param btn 按钮
 */
- (void)clickAction:(UIButton *)btn {
    switch (btn.tag) {
        case 10:                //back
        {
            [self hiddenWithAnimation:YES];
        }
            break;
        case 11:                //next
        {
            [self.drawer next];
        }
            break;
        case 12:                //last
        {
            [self.drawer last];
        }
            break;
        case 13:                //delete
        {
            [self.drawer clear];
        }
            break;
            
        default:
            break;
    }
}

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary<NSString *,id> *)change
                       context:(void *)context
{
    if([keyPath isEqualToString:@"lines"]){
        NSMutableArray * lines = [_myDrawer mutableArrayValueForKey:@"lines"];
        if (lines.count) {
            [self.clearBtn setEnabled:YES];
            [self.lastBtn setEnabled:YES];
        }else{
            [self.clearBtn setEnabled:NO];
            [self.lastBtn setEnabled:NO];
        }
    } else if([keyPath isEqualToString:@"canceledLines"]){
        NSMutableArray * canceledLines = [_myDrawer mutableArrayValueForKey:@"canceledLines"];
        [self.nextBtn setEnabled:canceledLines.count != 0 ? YES : NO];
    }
}

#pragma mark - ViewSignalProtocol
- (RACSignal *)getSignalWithDictionary:(NSDictionary *)dict {
    NSString *str = dict[@"action"];
    if (!str)   return [RACSignal empty];
    
    return [RACSignal empty];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
